$(function() {
    initRuleTypeEvent();
    initSubmitEvent();
});

/**
 * 表单校验集成
 */
function initSubmitEvent(){
    $('#submitForm').validate({
        debug: true, //调试模式取消submit的默认提交功能
        focusInvalid: false,
        onkeyup: false,
        submitHandler: function(form){
            if (typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
                jQuery.data(this, "disabledOnSubmit", {submited : true});
                //提交前设置默认值
                setDefaultValue();
                form.submit();
            } else {
                // repeat submit ignore;
            }
        },
        success:function(element){
        },
        errorPlacement : function(error, element) {
            if(error.text()){
                if(element.next("span[class='error red']").size() == 0){
                    element.after("<span class='error red'>" + error.text() +"</span>");
                }else{
                    element.next("span[class='error red']").text(error.text());
                }
            }else{
                element.next("span[class='error red']").remove();
            }
        },
        rules:{
            name:{required:true},
            minPromiseMoney:{conditionRequired:['[name=ruleType]',['marginScope']]},
            maxPromiseMoney:{conditionRequired:['[name=ruleType]',['marginScope']],isFloatGteThen:["#minPromiseMoney"]},
            keepGoodsNum:{conditionRequired:['[name=ruleType]',['marginScope']],min:1,max:1000,digits:true},
            biddingPeriod:{conditionRequired:(function(){
                    if($("input[name='biddingPeriodOption']:checked").val() == "1"){
                        return true;
                    }
                }),min:1,digits:true,max:99999
            },
            ruleType:{required:true},
            bidModel:{required:true},
            endTime:{required:true}
        },
        messages:{
            name:{required:"规则名称必填"},
            minPromiseMoney:{required:"最小保证金必填"},
            maxPromiseMoney:{required:"最大保证金必填",isFloatGteThen:"最大保证金必须大于或等于最小保证金"},
            keepGoodsNum:{required:"保持商品数必填"},
            biddingPeriod:{conditionRequired:"竞购周期时间必填"},
            endTime:{required:"结束生效时间必填"}
        }
    });
}

/**
 * 注册规则模式的选择事件
 */
function initRuleTypeEvent() {
	var defaultRuleType = $("[name=ruleType]").val();
    selectRuleTypePane(defaultRuleType?defaultRuleType:'marginScope');
    $("[name=ruleType]").bind("click",function(){
        selectRuleTypePane($(this).val());
    });
}

/**
 * 根据规则模式不同选择不同form
 * @param ruleType
 */
function selectRuleTypePane(ruleType) {
    $(".dynamic").hide();
    $(".dynamic." + ruleType).show();
    //查找到展示的未选中竞购模式，默认勾选上第一个
    var checkedRuleType = $("input[name=bidModel]:checked");
    if (!checkedRuleType.is(':visible')) {//元素不展示，默认选择一个
        $("input[name=bidModel]:visible").first().attr('checked','true');
    }
    $("form").find("td span[class='error red']").remove();
}
function setDefaultValue(){
	if($("input[name='biddingPeriodOption']:checked").val() == "0"){
		$("input[name='biddingPeriod']").val("");
	}
}


function checkvalue(obj){
	obj.value = obj.value.replace(/[^\d.]/g,""); //清除"数字"和"."以外的字符
	obj.value = obj.value.replace(/^\./g,""); //验证第一个字符是数字
	obj.value = obj.value.replace(/\.{2,}/g,"."); //只保留第一个, 清除多余的
	obj.value = obj.value.replace(".","$#$").replace(/\./g,"").replace("$#$",".");
	obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3'); //只能输入两个小数	
}
